<?php 
/*
* 侧边栏组件、页面模块
*/
if(!defined('EMLOG_ROOT')) {exit('error!');} 
?>
<?php
//widget：blogger
function widget_blogger($title){
	global $CACHE;
	$user_cache = $CACHE->readCache('user');
	$name = $user_cache[1]['mail'] != '' ? "<a href=\"mailto:".$user_cache[1]['mail']."\">".$user_cache[1]['name']."</a>" : $user_cache[1]['name'];?>
	<div class="widget-container rightwidget">
    <h3 class="widget-title">
     <span class="title"><?php echo $title; ?></span></h3>
	<div id="widget-box">
	<?php if (!empty($user_cache[1]['photo']['src'])): ?>
	<img src="<?php echo BLOG_URL.$user_cache[1]['photo']['src']; ?>" width="<?php echo $user_cache[1]['photo']['width']; ?>" height="<?php echo $user_cache[1]['photo']['height']; ?>" alt="blogger" />
	<?php endif;?>
	<p><b><?php echo $name; ?></b>
	<?php echo $user_cache[1]['des']; ?></p>
	</div>
   </div>
<?php }?>
<?php
//widget：日历
function widget_calendar($title){ ?>
	<div class="widget-container rightwidget">
    <h3 class="widget-title">
     <span class="title"><?php echo $title; ?></span></h3>
	<div id="calendar">
	</div>
	<script>sendinfo('<?php echo Calendar::url(); ?>','calendar');</script>
   </div>
<?php }?>
<?php
//widget：标签
function widget_tag($title){
	global $CACHE;
	$tag_cache = $CACHE->readCache('tags');?>
	<div class="widget-container rightwidget">
    <h3 class="widget-title">
     <span class="title"><?php echo $title; ?></span></h3>
	<div id="widget-box">
	<?php foreach($tag_cache as $value): ?>
		<span style="font-size:<?php echo $value['fontsize']; ?>pt; line-height:30px;">
		<a href="<?php echo Url::tag($value['tagurl']); ?>" title="<?php echo $value['usenum']; ?> 篇文章"><?php echo $value['tagname']; ?></a></span>
	<?php endforeach; ?>
	</div>
   </div>
<?php }?>
<?php
//widget：分类
function widget_sort($title){
	global $CACHE;
	$sort_cache = $CACHE->readCache('sort'); ?>
	<div class="widget-container sortwidget">
    <h3 class="widget-title">
     <span class="title"><?php echo $title; ?></span></h3>
	<div id="widget-box">
	<?php
	foreach($sort_cache as $value):
		if ($value['pid'] != 0) continue;
	?>
	<li>
	<a href="<?php echo Url::sort($value['sid']); ?>"><?php echo $value['sortname']; ?>(<?php echo $value['lognum'] ?>)</a>
	<?php if (!empty($value['children'])): ?>
		<ul>
		<?php
		$children = $value['children'];
		foreach ($children as $key):
			$value = $sort_cache[$key];
		?>
		<li>
			<a href="<?php echo Url::sort($value['sid']); ?>"><?php echo $value['sortname']; ?>(<?php echo $value['lognum'] ?>)</a>
		</li>
		<?php endforeach; ?>
		</ul>
	<?php endif; ?>
	</li>
	<?php endforeach; ?>
    </div>
   </div>
<?php }?>
<?php
//widget：最新碎语
function widget_twitter($title){
	global $CACHE; 
	$newtws_cache = $CACHE->readCache('newtw');
	$istwitter = Option::get('istwitter');
	?>
	<div class="widget-container rightwidget">
    <h3 class="widget-title">
     <span class="title"><?php echo $title; ?></span></h3>
	<div id="widget-box">
	<?php foreach($newtws_cache as $value): ?>
	<li><?php echo $value['t']; ?><p><?php echo smartDate($value['date']); ?> </p></li>
	<?php endforeach; ?>
    <?php if ($istwitter == 'y') :?>
	<p><a href="<?php echo BLOG_URL . 't/'; ?>">更多&raquo;</a></p>
	<?php endif;?>
	</div>
   </div>
<?php }?>
<?php
//widget：最新评论
function widget_newcomm($title){
	global $CACHE; 
	$com_cache = $CACHE->readCache('comment');
	?>
	<div class="widget-container rightwidget">
    <h3 class="widget-title">
     <span class="title"><?php echo $title; ?></span></h3>
	<div id="widget-box">
	<?php
	foreach($com_cache as $value):
	$url = Url::comment($value['gid'], $value['page'], $value['cid']);
	?>
	<li id="comment"><?php echo $value['name']; ?>
	<br /><a href="<?php echo $url; ?>"><?php echo $value['content']; ?></a></li>
	<?php endforeach; ?>
	</div>
   </div>
<?php }?>
<?php
//widget：最新文章
function widget_newlog($title){
	global $CACHE; 
	$newLogs_cache = $CACHE->readCache('newlog');
	?>
	<div class="widget-container rightwidget">
    <h3 class="widget-title">
     <span class="title"><?php echo $title; ?></span></h3>
	<div id="widget-box">
	<?php foreach($newLogs_cache as $value): ?>
	<li><a href="<?php echo Url::log($value['gid']); ?>"><?php echo $value['title']; ?></a></li>
	<?php endforeach; ?>
	</div>
   </div>
<?php }?>
<?php
//widget：热门文章
function widget_hotlog($title){
	$index_hotlognum = Option::get('index_hotlognum');
	$Log_Model = new Log_Model();
	$randLogs = $Log_Model->getHotLog($index_hotlognum);?>
	<div class="widget-container rightwidget">
    <h3 class="widget-title">
     <span class="title"><?php echo $title; ?></span></h3>
	<div id="widget-box">
	<?php foreach($randLogs as $value): ?>
	<li><a href="<?php echo Url::log($value['gid']); ?>"><?php echo $value['title']; ?></a></li>
	<?php endforeach; ?>
	</div>
   </div>
<?php }?>
<?php
//widget：随机文章
function widget_random_log($title){
	$index_randlognum = Option::get('index_randlognum');
	$Log_Model = new Log_Model();
	$randLogs = $Log_Model->getRandLog($index_randlognum);?>
	<div class="widget-container rightwidget">
    <h3 class="widget-title">
     <span class="title"><?php echo $title; ?></span></h3>
	<div id="widget-box">
	<?php foreach($randLogs as $value): ?>
	<li><a href="<?php echo Url::log($value['gid']); ?>"><?php echo $value['title']; ?></a></li>
	<?php endforeach; ?>
	</div>
   </div>
<?php }?>
<?php
//widget：搜索
function widget_search($title){ ?>
	<div class="widget-container rightwidget">
    <h3 class="widget-title">
     <span class="title"><?php echo $title; ?></span></h3>
	<div id="widget-box">
	<form name="keyform" method="get" action="<?php echo BLOG_URL; ?>index.php">
	<input name="q" class="search" type="text" id="s"/>
	</form>
	</div>
   </div>
<?php } ?>
<?php
//widget：归档
function widget_archive($title){
	global $CACHE; 
	$record_cache = $CACHE->readCache('record');
	?>
	<div class="widget-container rightwidget">
    <h3 class="widget-title">
     <span class="title"><?php echo $title; ?></span></h3>
	<div id="widget-box">
	<?php foreach($record_cache as $value): ?>
	<li><a href="<?php echo Url::record($value['date']); ?>"><?php echo $value['record']; ?>(<?php echo $value['lognum']; ?>)</a></li>
	<?php endforeach; ?>
	</div>
   </div>
<?php } ?>
<?php
//widget：自定义组件
function widget_custom_text($title, $content){ ?>
	<div class="widget-container rightwidget">
    <h3 class="widget-title">
     <span class="title"><?php echo $title; ?></span></h3>
	<div id="widget-box">
	<?php echo $content; ?>
	</div>
   </div>
<?php } ?>
<?php
//widget：链接
function widget_link($title){
	global $CACHE; 
	$link_cache = $CACHE->readCache('link');
	?>
	<div class="widget-container rightwidget">
    <h3 class="widget-title">
     <span class="title"><?php echo $title; ?></span></h3>
	<div id="widget-box">
	<?php foreach($link_cache as $value): ?>
	<li><a href="<?php echo $value['url']; ?>" title="<?php echo $value['des']; ?>" target="_blank"><?php echo $value['link']; ?></a></li>
	<?php endforeach; ?>
	</div>
   </div>
<?php }?>
<?php
//blog：导航
function blog_navi(){
	global $CACHE; 
	$navi_cache = $CACHE->readCache('navi');
	?>
	<?php 
	foreach($navi_cache as $value):
		if($value['url'] == 'admin' && (ROLE == 'admin' || ROLE == 'writer')):
			?>
			<?php 
			continue;
		endif;
		$newtab = $value['newtab'] == 'y' ? 'target="_blank"' : '';
		$value['url'] = $value['isdefault'] == 'y' ? BLOG_URL . $value['url'] : trim($value['url'], '/');
		$current_tab = (BLOG_URL . trim(Dispatcher::setPath(), '/') == $value['url']) ? 'current' : 'common';
		?>
		<li class="<?php echo $current_tab;?>"><a href="<?php echo $value['url']; ?>" <?php echo $newtab;?>><?php echo $value['naviname']; ?></a></li>
	<?php endforeach; ?>
<?php }?>
<?php
//blog：置顶
function topflg($istop){
	$topflg = $istop == 'y' ? "<img src=\"".TEMPLATE_URL."images/top.png\" title=\"站长推荐\" /> " : '';
	echo $topflg;
}
?>
<?php
//blog：编辑
function editflg($logid,$author){
	$editflg = ROLE == 'admin' || $author == UID ? '<a href="'.BLOG_URL.'admin/write_log.php?action=edit&gid='.$logid.'">编辑</a>' : '';
	echo $editflg;
}
?>
<?php
//blog：分类
function blog_sort($blogid){
	global $CACHE; 
	$log_cache_sort = $CACHE->readCache('logsort');
	?>
	<?php if(!empty($log_cache_sort[$blogid])): ?>
	<a href="<?php echo Url::sort($log_cache_sort[$blogid]['id']); ?>" title="点击查看“<?php echo $log_cache_sort[$blogid]['name']; ?>”的文章"><?php echo $log_cache_sort[$blogid]['name']; ?></a>
	<?php endif;?>
<?php }?>
<?php
//blog：文章标签
function blog_tag($blogid){
	global $CACHE;
	$log_cache_tags = $CACHE->readCache('logtags');
	if (!empty($log_cache_tags[$blogid])){
		$tag = '标签:';
		foreach ($log_cache_tags[$blogid] as $value){
			$tag .= "	<a href=\"".Url::tag($value['tagurl'])."\">".$value['tagname'].'</a>';
		}
		echo $tag;
	}
}
?>
<?php
//blog：文章作者
function blog_author($uid){
	global $CACHE;
	$user_cache = $CACHE->readCache('user');
	$author = $user_cache[$uid]['name'];
	$mail = $user_cache[$uid]['mail'];
	$des = $user_cache[$uid]['des'];
	$title = !empty($mail) || !empty($des) ? "title=\"$des $mail\"" : '';
	echo '<a href="'.Url::author($uid)."\" $title>$author</a>";
}
?>
<?php
//blog：相邻文章
function neighbor_log($neighborLog){
	extract($neighborLog);?>
	<?php if($prevLog):?>
	NEXT&raquo;<a href="<?php echo Url::log($prevLog['gid']) ?>"><?php echo $prevLog['title'];?></a>
	<?php endif;?>
	<?php if($nextLog && $prevLog):?>
	<br/>
	<?php endif;?>
	<?php if($nextLog):?>
	PREV&raquo;<a href="<?php echo Url::log($nextLog['gid']) ?>"><?php echo $nextLog['title'];?></a>
	<?php endif;?>
<?php }?>
<?php
//blog：引用通告
function blog_trackback($tb, $tb_url, $allow_tb){
    if($allow_tb == 'y' && Option::get('istrackback') == 'y'):?>
	<div id="trackback_address">
	<p>引用地址:<?php echo $tb_url; ?>
	<a name="tb"></a></p>
	</div>
	<?php endif; ?>
	<?php foreach($tb as $key=>$value):?>
		<ul id="trackback">
		<li><a href="<?php echo $value['url'];?>" target="_blank"><?php echo $value['title'];?></a></li>
		<li>BLOG: <?php echo $value['blog_name'];?></li><li><?php echo $value['date'];?></li>
		</ul>
	<?php endforeach; ?>
<?php }?>
<?php 
//首页分类日志函数
	function home_sort_list($sortname,$sort1,$sort2,$sort3,$sort4,$sort5,$sort6,$sort7){
				?>
<div id="middle">
<div id="left">
  <div class="listing">
   <?php
		$log_Model = new Log_Model;
		$logs = $log_Model->getLogsForHome("and (sortid=$sort1 or sortid=$sort2 or sortid=$sort3 or sortid=$sort4 or sortid=$sort5 or sortid=$sort6 or sortid=$sort7) and top!='y' ORDER BY `date` DESC", 0, 11);
		$logs1 = array_slice($logs,0,1);
	?>
    <div class="header">
      <h1><?php echo $sortname;?></h1>
    </div><!-- #header -->
      <div class="content">
        <div class="left">
           <div class="imgholder">
		   <?php 
		   foreach($logs1 as $row){?>
             <a href="<?php echo Url::log($row['gid']);?>" title="<?php echo $row['title'];?>" target="_blank">
			 <img src="<?php logs_thumb($row,'276','202'); ?>" 
			 alt="<?php echo $row['title'];?>" class="imgf"  border="0" style="height:208px;" /></a>
                <div class="description">
				 <h3><a href="<?php echo Url::log($row['gid']);?>" title="<?php echo $row['title'];?>"><?php echo extractHtmlData($row['title'],16); ?></a></h3>
				</div><!-- #description -->
			<?php }?>
            </div><!-- #imgholder -->
        </div><!-- #left -->
        <div class="right">
		<?php 
		$logs2 = array_slice($logs,1,3);
		   foreach($logs2 as $row){?>
          <div class="small-listing">
            <div class="thumb">
              <a href="<?php echo Url::log($row['gid']);?>" title="<?php echo $row['title'];?>" target="_blank">
			  <img src="<?php logs_thumb($row,'106','69'); ?>" 
			  alt="<?php echo $row['title'];?>" class="shareimg" border="0" /></a>
            </div><!-- #thumb -->
            <div class="description">
			<h4><a href="<?php echo Url::log($row['gid']);?>" title="<?php echo $row['title'];?>" target="_blank"><?php echo extractHtmlData($row['title'],12); ?></a></h4>
			<span class="date">发表于：<?php echo gmdate('y-m-d', $row['date']);?></span>
			<span class="date"><?php blog_tag($row['gid']); ?></span>
			</div><!-- #description -->
          </div><!-- #small-listing -->
		<?php }?>
        </div><!-- #right -->
      </div><!-- #content -->
  </div><!-- #listing -->
</div><!-- #left closer -->
<div id="right-x">
   <div class="widget-container rightwidget">
    <h3 class="widget-title">
     <span class="title">more</span></h3>
       <div id="widget-box">
	   <?php 
		   $logs3 = array_slice($logs,4,11);
		   foreach($logs3 as $row){?>
		   <li><a href="<?php echo Url::log($row['gid']);?>" title="<?php echo $row['title'];?>" target="_blank"><?php echo extractHtmlData($row['title'],40); ?></a></li>
		   <?php }?>
       </div>
   </div>
</div>
</div><!-- #middle-->
<?php }?>
<?php
//blog-tool:判断是否是首页
function blog_tool_ishome(){
    if (BLOG_URL . trim(Dispatcher::setPath(), '/') == BLOG_URL){
        return true;
    } else {
        return FALSE;
    }
}
?>